<template>
  <n-popover style="background-color: #292929; color: white" trigger="hover">
    <template #trigger>
      <slot name="trigger"></slot>
    </template>
    <div class="min-w-[150px] max-w-[260px] flex flex-col gap-2 rounded-sm">
      <slot name="deviceLabel"></slot>
      <div
        v-for="(item, index) in deviceList"
        :key="index"
        class="w-full hover:bg-[#2d2d2d] hover:text-sky-400 p-2 cursor-pointer text-fontSecondary"
        @click="emit('changeDevice', item.deviceId)"
      >
        <div :class="activeDeviceId === item.deviceId ? 'text-sky-600' : ''" class="flex flex-row items-center gap-2">
          <el-icon v-if="activeDeviceId === item.deviceId"><Select /></el-icon>
          <p class="line-clamp-1">{{ item.label }}</p>
        </div>
      </div>
    </div>
  </n-popover>
</template>

<script lang="ts" setup>
import { NPopover } from "naive-ui";
import { Select } from "@element-plus/icons-vue";

const emit = defineEmits<{
  (event: "changeDevice", deviceId: string): void;
}>();
withDefaults(
  defineProps<{
    deviceList: MediaDeviceInfo[];
    activeDeviceId: string;
  }>(),
  {
    deviceList: () => [],
    activeDeviceId: "",
  },
);
</script>

<style lang="scss" scoped></style>
